<?php

defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed.' );

/**
 * CodeIgniter Language Helpers
 *
 * @package		CodeIgniter
 * @subpackage	Helpers
 * @category	Helpers
 * @author		PyroCMS development team
 */

// ------------------------------------------------------------------------

function fetch_lang($lang_data){
	$langs = array();
	$supported_languages = ci()->config->item('supported_languages');
	foreach($supported_languages as $lang => $lang_content)
	{
		$langs[$lang] = array_pop(array_filter($lang_data, create_function('$row','return $row->lang == "'. $lang .'";')));
	}
	return $langs;
}
function get_supported_lang() {
	$CI = & get_instance ();
	$supported_lang = Settings_lib::get ( 'supported_languages' );
	$arr = array ();
	foreach ( $supported_lang as $key => $lang ) {
		$arr [] = $key . '=' . $lang ['name'];
	}
	
	return $arr;
}

function def_lang($obj, $key, $lang = FALSE) {

	if(!isset($obj->langs)){
		return '';
	}
	if(FALSE === $lang){
		$cur_lang = $obj->langs[CURRENT_LANGUAGE];
	}else{
		$cur_lang = $obj->langs[$lang];
	}
	if(!isset($cur_lang->{$key})){
		return '';
	}
	return $cur_lang->{$key};
}

function lang($line) {
	$CI = & get_instance ();
	$line = $CI->lang->line ( $line );
	
	$args = func_get_args ();
	
	if (is_array ( $args ))
		array_shift ( $args );
	if (is_array ( $args ) && count ( $args )) {
		foreach ( $args as $arg ) {
			$line = str_replace_first ( '%s', $arg, $line );
		}
	}
	return $line;
}

function str_replace_first($search_for, $replace_with, $in) {
	$pos = strpos ( $in, $search_for );
	if ($pos === false) {
		return $in;
	} else {
		return substr ( $in, 0, $pos ) . $replace_with . substr ( $in, $pos + strlen ( $search_for ), strlen ( $in ) );
	}
}